package com.hxkj.client.service.taglib.impl;

import com.hxkj.domain.po.FreeMarker;
import com.hxkj.client.service.LabelDirectiveService;
import com.hxkj.client.service.taglib.AnalyzeChannelService;
import com.hxkj.utils.MD5Utils;
import com.integral.mall.file.po.SysFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @ClassName AnalyzeColumnService
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/7/18 17:24
 * @Version 1.0
 */
@Service
@Slf4j
public class AnalyzeChannelServiceImpl implements AnalyzeChannelService {

    @Autowired
    private LabelDirectiveService labelDirectiveService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public SysFile analyzeChannel(FreeMarker freeMarker) throws Exception {
        return getSysFileResultDataStruct(freeMarker);
    }

    @Override
    public SysFile analyzeHotArticle(FreeMarker freeMarker) throws Exception {
        return getSysFileResultDataStruct(freeMarker);
    }

    public SysFile getSysFileResultDataStruct(FreeMarker freeMarker) throws Exception {
        String userToken = "";
        if(!StringUtils.isEmpty(freeMarker.getNote())) {
            userToken = freeMarker.getUserToken() + "_" + MD5Utils.getMd5ByTemplte();
            freeMarker.setUserToken(userToken);
        }
        SysFile file = labelDirectiveService.labelDirective(freeMarker);
        if(!StringUtils.isEmpty(userToken)) {
            redisTemplate.delete(userToken);
        }
        return file;
    }
}
